package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChatBean;
import com.uchengwang.ui.cm_ui.umsg.http.ChatHttpManager;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2017/12/26.
 * 享聊我的界面
 */

public class WantMeActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private ActivityTitleBar titleBar;
    private ImageView head_bigImg, head_1, head_2;
    private LinearLayout editor_img_ln;//编辑头像
    private RelativeLayout name_rl, level_rl;
    private TextView name, levelTv, noPass, schoolTv, signTv, wantMe_line;
    private ImageView sexImage, level_img;
    private LinearLayout lever_right_2, sign_rl;//审核没有通过
    private WantChatBean bean;
    private int examineType;//-1=未通过 0=待审核 1=通过
    private boolean isActivi;
    private List<String> imgs;

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (isActivi){
                DialogUtils.getUtils().showDialogSingle(WantMeActivity.this, "关闭后, 您的享聊数据将被清除, 会导致无法使用享聊功能, 确定关闭吗?", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DialogUtils.getUtils().cancel();
                        closeOrOpen();
                    }
                });
            }else {
                //开启享聊
                closeOrOpen();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_want_chat_me);
        EventBus.getDefault().register(this);
        setTitle("个人资料");

        initView();

    }

    private void initView() {
        titleBar = (ActivityTitleBar) findViewById(R.id.wantMe_titleBar);
        titleBar.setVisibility(View.GONE);
        wantMe_line = (TextView) findViewById(R.id.wantMe_line);
        wantMe_line.setVisibility(View.GONE);

        head_bigImg = (ImageView) findViewById(R.id.wantMe_head_bigImg);//三个头像
        head_1 = (ImageView) findViewById(R.id.wantMe_head_1);
        head_2 = (ImageView) findViewById(R.id.wantMe_head_2);

        editor_img_ln = (LinearLayout) findViewById(R.id.wantMe_editor_img_ln);//点击编辑头像
        name_rl = (RelativeLayout) findViewById(R.id.wantMe_name_rl);
        name = (TextView) findViewById(R.id.wantMe_name);
        sexImage = (ImageView) findViewById(R.id.wantMe_sex);

        level_rl = (RelativeLayout) findViewById(R.id.wantMe_level_rl);//魅力值显示
        levelTv = (TextView) findViewById(R.id.wantMe_level);
        level_img = (ImageView) findViewById(R.id.wantMe_level_img);
        lever_right_2 = (LinearLayout) findViewById(R.id.lever_right_2);
        noPass = (TextView) findViewById(R.id.wantMe_noPass);

        schoolTv = (TextView) findViewById(R.id.wantMe_school);

        sign_rl = (LinearLayout) findViewById(R.id.wantMe_sign_rl);

        signTv = (TextView) findViewById(R.id.wantMe_sign);


        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);

        getInfo();

    }

    /**
     * 获取自己的享聊信息
     * */
    private void getInfo(){
        ChatHttpManager.getManager().getMyChatInfo(this, 1, new OnHttpResultSingleListener<WantChatBean>() {
            @Override
            public void onResult(int status, String msg, WantChatBean wantChatBean) {
                bean = wantChatBean;
                initDataView();
            }
        });
    }
    private void initDataView() {
        if (bean != null){
            isActivi = bean.isActive();
            setRightText(isActivi ? "关闭" : "开启", listener);

            imgs = bean.getHeads();
            if (imgs != null && imgs.size() >0){
                for (int i=0; i<imgs.size(); i++){
                    String url = imgs.get(i);
                    switch (i){
                        case 0:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, url, ImageManager.ImageType.ROUND);
                                head_bigImg.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                        case 1:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, url, ImageManager.ImageType.ROUND);
                                head_1.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                        case 2:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, url, ImageManager.ImageType.ROUND);
                                head_2.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                    }

                }
            }
            name.setText(bean.getChatName());
            int sex = bean.getSex();//0=女 1=男
            if (sex ==0){
                ImageManager.getInstance().loadImage(this, sexImage, R.drawable.icon_want_chat_woman, ImageManager.ImageType.NARMOL);
            }else {
                ImageManager.getInstance().loadImage(this, sexImage, R.drawable.icon_want_chat_man, ImageManager.ImageType.NARMOL);
            }

            examineType = bean.getIsExamine();
            switch (examineType){//-1=未通过 0=待审核 1=通过
                case -1:
                    levelTv.setText("未通过");
                    lever_right_2.setVisibility(View.VISIBLE);
                    level_rl.setOnClickListener(this);
                    noPass.setVisibility(View.VISIBLE);
                    break;
                case 0:
                    levelTv.setText("审核中");
                    editor_img_ln.setOnClickListener(this);
                    sign_rl.setOnClickListener(this);
                    noPass.setVisibility(View.GONE);
                    break;
                case 1:
                    level_img.setVisibility(View.VISIBLE);
                    levelTv.setText("1");
                    editor_img_ln.setOnClickListener(this);
                    sign_rl.setOnClickListener(this);
                    break;
            }

            schoolTv.setText(bean.getUniName());
            signTv.setText(bean.getChatSign());
        }
    }

    /**
     * 开启关闭享聊
     * */
    private void closeOrOpen(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.IS_ACTIVI, !isActivi);
        map.put(BaseConfig.CHAT_TYPE, 1);
        ChatHttpManager.getManager().changeWantInfo(this, map, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (stutas ==0){
                    isActivi = !isActivi;
                    bean.setActive(isActivi);
//                    setRightText(isActivi ? "关闭" : "开启", listener);
                    Intent intent  = new Intent();
                    intent.putExtra(BaseConfig.BEAN, bean);
                    setResult(1, intent);
                    WantMeActivity.this.finish();
                }else {
                    UCommonUtil.getUtil().showToast(WantMeActivity.this, "请重试");
                }
            }
        });
    }

    /**
     * 收到信息
     * */
    public void onEvent( WantChatBean b){
        if (b != null){
            bean = b;
            initDataView();
        }
    }


    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()){
            case R.id.wantMe_head_bigImg:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 0, false);
                break;
            case R.id.wantMe_head_1:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 1, false);
                break;
            case R.id.wantMe_head_2:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 2, false);
                break;

            case R.id.wantMe_editor_img_ln://编辑头像
                if (examineType == 0){
                    UCommonUtil.getUtil().showToast(this, "请耐心等待, 正在审核中...");
                    return;
                }
                intent = new Intent(this, WantChatEditorImgActivity.class);
                intent.putExtra(BaseConfig.BEAN, bean);
                intent.putExtra(BaseConfig.TYPE, 1);
                break;
            case R.id.wantMe_name_rl://点击名字
                break;
            case R.id.wantMe_level_rl://点击魅力值那一栏 重新申请
                if (examineType == 0){
                    UCommonUtil.getUtil().showToast(this, "请耐心等待, 正在审核中...");
                    return;
                }
                intent = new Intent(this, WantChatEditorImgActivity.class);
                intent.putExtra(BaseConfig.BEAN, bean);
                intent.putExtra(BaseConfig.TYPE, 2);
                break;
            case R.id.wantMe_sign_rl://点击标语那一栏
                if (examineType == 0){
                    UCommonUtil.getUtil().showToast(this, "请耐心等待, 正在审核中...");
                    return;
                }
                Intent i = new Intent(this, EditorWantChatInfoActivity.class);
                i.putExtra(BaseConfig.TYPE, 2);
                if (bean != null){
                    i.putExtra(BaseConfig.CONTENT, bean.getChatSign());
                }
                startActivityForResult(i, 2);
                break;
        }

        if (intent != null){
            startActivity(intent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null){
            int t = data.getIntExtra(BaseConfig.TYPE, 0);
            String con = data.getStringExtra(BaseConfig.CONTENT);
            switch (t){//1编辑享聊的昵称  2编辑享聊的宣言
                case 1:

                    break;
                case 2:
                    signTv.setText(con);
                    if (bean != null){
                        bean.setChatSign(con);
                    }
                    break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
